<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-8-3
 * Time: 16:58
 */

namespace App\Repositories;



use App\Models\ClassConfig;

class ClassConfigRepository extends BaseRepository{

    public function __construct(ClassConfig $classConfig)
    {
        $this->model = $classConfig;
    }

    public function index($data, $extra = '', $size = 10)
    {

    }

    /**
     * 添加
     * @param array $inputs
     * @param string $extra
     * @return mixed
     */
    public function store($inputs = [], $extra = '')
    {
        $model = new $this->model;
        $inputs = self::jurisdictionDispose($inputs);
        return $this->save($model,$inputs);
    }


    /**
     * @param int $id
     * @param string $extra
     * @return int
     */
    public function destroy($id, $extra = '')
    {
        $list = $this->getById($id);
        return $list->delete();
    }

    /**
     * @param $id
     * @return mixed
     */
    public function getByConfig($id)
    {
        return $this->model->where('id',$id)->first();
    }

    /**
     * 修改
     * @param int $id
     * @param array $inputs
     * @param string $extra
     * @return mixed
     */
    public function update($id, $inputs, $extra = '')
    {
        $model = $this->getById($id);
        $inputs = self::jurisdictionDispose($inputs);
        return $this->save($model,$inputs);
    }

    /**
     * 处理一下权限
     * @param $inputs
     * @return mixed
     */
    public function jurisdictionDispose($inputs)
    {
        if(isset($inputs['stu_putime_way'])){
            if($inputs['stu_putime_way']==1){
                $inputs['stu_putime_times'] = $inputs['stu_putime_times_1'];
                $inputs['stu_putime_timestype'] = $inputs['stu_putime_timestype_1'];
            }elseif($inputs['stu_putime_way']==2){
                $inputs['stu_putime_times'] = $inputs['stu_putime_times_2'];
                $inputs['stu_putime_timestype'] = $inputs['stu_putime_timestype_2'];
            }

        }
        if(isset($inputs['stu_putime_cancel_way'])){
            if($inputs['stu_putime_cancel_way']==1){
                $inputs['stu_putime_cancel_times'] = $inputs['stu_putime_cancel_times_1'];
                $inputs['stu_putime_cancel_timestype'] = $inputs['stu_putime_cancel_timestype_1'];
            }elseif($inputs['stu_putime_cancel_way']==2){
                $inputs['stu_putime_cancel_times'] = $inputs['stu_putime_cancel_times_2'];
                $inputs['stu_putime_cancel_timestype'] = $inputs['stu_putime_cancel_timestype_2'];
            }
        }
        return $inputs;
    }

    /**
     * @param $model
     * @param $inputs
     * @return mixed
     */
    public function save($model, $inputs)
    {
        if (array_key_exists('school_id',$inputs)) {
            $model->school_id = e($inputs['school_id']);//驾校id
        }
        if (array_key_exists('service_id',$inputs)) {
            $model->service_id = e($inputs['service_id']);//收费标准id
        }
        //理论学习权限
        if (array_key_exists('stu_video',$inputs)) {
            $model->stu_video = e($inputs['stu_video']);//视频开关 1：开启 0：关闭
        }
        if (array_key_exists('stu_video_order',$inputs)) {
            $model->stu_video_order = e($inputs['stu_video_order']);//课件是否按照顺序
        }
        if (array_key_exists('stu_video_drag',$inputs)) {
            $model->stu_video_drag = e($inputs['stu_video_drag']);//是否开启拖放
        }
        if (array_key_exists('stu_video_h',$inputs)) {
            $model->stu_video_h = e($inputs['stu_video_h']);//1天最多学习视频个数
        }
        //学时预约权限
        if (array_key_exists('stu_putime',$inputs)) {
            $model->stu_putime = e($inputs['stu_putime']);//预约学车开关 1打开  0关闭
        }
        if (array_key_exists('stu_putime_needcomment',$inputs)) {
            $model->stu_putime_needcomment = e($inputs['stu_putime_needcomment']);//是否需要评价才能继续学车
        }
        if (array_key_exists('stu_putime_order',$inputs)) {
            $model->stu_putime_order = e($inputs['stu_putime_order']);//是否需要按科目1-科目3顺序
        }
        if (array_key_exists('stu_putime_pickup',$inputs)) {
            $model->stu_putime_pickup = e($inputs['stu_putime_pickup']);//是否需要接送
        }
        if (array_key_exists('stu_putime_pickup_number',$inputs)) {
            $model->stu_putime_pickup_number = e($inputs['stu_putime_pickup_number']);//接送次数
        }
        if (array_key_exists('stu_putime_theday',$inputs)) {
            $model->stu_putime_theday = e($inputs['stu_putime_theday']);//能否预约当天的学时
        }
        if (array_key_exists('stu_putime_nearday',$inputs)) {
            $model->stu_putime_nearday = e($inputs['stu_putime_nearday']);//显示近几天的排班
        }
        if (array_key_exists('stu_putime_update_H',$inputs)) {
            $model->stu_putime_update_H = e($inputs['stu_putime_update_H']);//几点更新排班
        }
        if (array_key_exists('stu_putime_way',$inputs)) {
            $model->stu_putime_way = e($inputs['stu_putime_way']);//预约方式 1：按现实时间  2：按操作时间
        }
        if (array_key_exists('stu_putime_times',$inputs)) {
            $model->stu_putime_times = e($inputs['stu_putime_times']);//最多预约次数
        }
        if (array_key_exists('stu_putime_timestype',$inputs)) {
            $model->stu_putime_timestype = e($inputs['stu_putime_timestype']);//预约次数限制类型 按1:天、2:周、3:月（D W M）
        }
        //学时取消权限
        if (array_key_exists('stu_putime_cancel',$inputs)) {
            $model->stu_putime_cancel = e($inputs['stu_putime_cancel']);//是否可以取消预约 1打开  0关闭
        }
        if (array_key_exists('stu_putime_cancel_way',$inputs)) {
            $model->stu_putime_cancel_way = e($inputs['stu_putime_cancel_way']);//取消预约方式 1：按现实时间  2：按操作时间
        }
        if (array_key_exists('stu_putime_cancel_H',$inputs)) {
            $model->stu_putime_cancel_H = e($inputs['stu_putime_cancel_H']);//取消预约提前小时数
        }
        if (array_key_exists('stu_putime_cancel_times',$inputs)) {
            $model->stu_putime_cancel_times = e($inputs['stu_putime_cancel_times']);//取消次数
        }
        if (array_key_exists('stu_putime_cancel_timestype',$inputs)) {
            $model->stu_putime_cancel_timestype = e($inputs['stu_putime_cancel_timestype']);//取消预约次数限制类型 按天、周、月（D W M）
        }
        //其他权限
        if (array_key_exists('stu_putime_surplus',$inputs)) {
            $model->stu_putime_surplus = e($inputs['stu_putime_surplus']);//剩余学时处理方式
        }
        if (array_key_exists('stu_comment',$inputs)) {
            $model->stu_comment = e($inputs['stu_comment']);//评论开关
        }
        $model->save();
        return $model;

    }

    /**
     * 获取服务项目权限信息
     * @param $school_id
     * @param $servive_id
     * @return mixed
     */
    public function getByClassConfigInfo($school_id, $servive_id)
    {
        return $this->model->where('school_id',$school_id)->where('service_id',$servive_id)->first();
    }

    /**
     * @param int $id
     * @param array|string $extra
     */
    public function edit($id,$extra)
    {
        // TODO: Implement edit() method.
    }


}